#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

/**
 * mat中数据如何遍历
 */
int main()
{
    Mat img = imread("../resource/wukong.jpg",IMREAD_GRAYSCALE);
    if (img.empty())
    {
        cout << "Image load failed!" << endl;
        return -1;
    }

    int w = img.cols;
    int h = img.rows;
    int dims = img.channels();

    for (int i=0;i<h;i++) {
        for (int j=0;j<w;j++) {
            if (dims == 1) {
                int pv = img.at<uchar>(i,j); //at有不同的重载，可以返回不同的数据类型
                img.at<uchar>(i,j) = 255 - pv;
            }
            else if (dims==3) {
                cv::Vec3b &pv = img.at<cv::Vec3b>(i,j);
                img.at<cv::Vec3b>(i,j)[0] = 255 - pv[0];
                img.at<cv::Vec3b>(i,j)[1] = 255 - pv[1];
                img.at<cv::Vec3b>(i,j)[2] = 255 - pv[2];
            }
        }
    }

    cv::imshow("img", img);
    cv::waitKey(0);
    cv::destroyAllWindows();
    
    return 0;
}